package com.css.zfzx.sjcj.modules.drill.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.utils.*;
import com.css.zfzx.sjcj.common.utils.CheckObjFields;
import com.css.zfzx.sjcj.common.utils.ServerUtil;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY02Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY08Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.vo.QhY08Vo;
import com.css.zfzx.sjcj.modules.drill.repository.QhY02Repository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY08NativeRepository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY08Repository;
import com.css.zfzx.sjcj.modules.drill.service.QhService;
import com.css.zfzx.sjcj.modules.drill.service.QhY08Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Li Helei
 * @date 2021/1/16 10:44
 */
@Slf4j
@Service
public class QhY08ServiceImpl implements QhY08Service {

	@Autowired
    private QhY08Repository qhY08Repository;
    @Autowired
    private QhY08NativeRepository qhY08NativeRepository;
    @Autowired
    private QhService qhService;
    @Override
    public JSONObject queryQhY08s(String drillId, int curPage, int pageSize,String sort,String order) {
        if (!PlatformObjectUtils.isEmpty(sort) && sort.length() > 0){
            sort= CheckObjFields.propertyChange(sort);
        }
        Page<QhY08Entity> qhY08Page = this.qhY08NativeRepository.queryQhY08s(drillId, curPage, pageSize,sort,order);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(qhY08Page);
        return jsonObject;
    }

    @Override
    public List<QhY08Entity> findByDrillId(String drillId) {
        return this.qhY08Repository.findByIsValidAndDrillIdOrderByCode("1", drillId);
    }

    /*@Override
    public JSONObject queryQhY08s(QhY08QueryParams queryParams) {
        List<QhY08Entity> qhY08List = this.qhY08NativeRepository.listQhY08s(queryParams);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", qhY08List);
        return jsonObject;
    }*/


    @Override
    public QhY08Entity getQhY08(String id) {
        QhY08Entity qhY08 = this.qhY08Repository.findByIdAndIsValid(id,"1");
         return qhY08;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY08Entity saveQhY08(QhY08Entity qhY08) {
        String uuid = UUIDGenerator.getUUID();
        qhY08.setId(uuid);
        qhY08.setIsValid("1");
        this.qhY08Repository.save(qhY08);
        try {
            qhService.update(qhY08);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qhY08;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY08Entity updateQhY08(QhY08Entity qhY08) {
        this.qhY08Repository.save(qhY08);
        try {
            qhService.update(qhY08);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qhY08;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQhY08s(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY08Entity> qhY08List = this.qhY08Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY08List) && qhY08List.size() > 0) {
            for(QhY08Entity qhY08 : qhY08List) {
                qhY08.setIsValid("0");
                this.qhY08Repository.save(qhY08);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitQhY08(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY08Entity> qhY08List = this.qhY08Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY08List) && qhY08List.size() > 0) {
            for(QhY08Entity qhY08 : qhY08List) {
                this.qhY08Repository.save(qhY08);
            }
        }
    }

    @Override
    public List<QhY08Vo> dataQualityCheck(String drillId) {
        JSONObject res = new JSONObject();
        List<QhY08Vo> qhY08VoList=new ArrayList<>();
        try {
            List<QhY08Entity> qhY08List=qhY08Repository.findByIsValidAndDrillIdOrderByCode("1", drillId);
            if(!PlatformObjectUtils.isEmpty(qhY08List) && qhY08List.size() > 0) {
                for (QhY08Entity qhY08 : qhY08List) {
                    QhY08Vo qhY08Vo=new QhY08Vo();
                    qhY08Vo.setId(qhY08.getId());
                    StringBuilder errorMsg = new StringBuilder();

                    //检查字段属性
                    String specimenCode=qhY08.getSpecimenCode();
                    Double specimenStartDepth=qhY08.getSpecimenStartDepth();
                    Double specimenEndDepth=qhY08.getSpecimenEndDepth();
                    String soilName=qhY08.getSoilName();
                    Double naturalDensity=qhY08.getNaturalDensity();
                    Double modulusShearStrain005=qhY08.getModulusShearStrain005();
                    Double modulusShearStrain010=qhY08.getModulusShearStrain010();
                    Double modulusShearStrain050=qhY08.getModulusShearStrain050();
                    Double modulusShearStrain100=qhY08.getModulusShearStrain100();
                    Double modulusShearStrain500=qhY08.getModulusShearStrain500();
                    Double modulusShearStrain1000=qhY08.getModulusShearStrain1000();
                    Double modulusShearStrain5000=qhY08.getModulusShearStrain5000();
                    Double modulusShearStrain10000=qhY08.getModulusShearStrain10000();
                    Double dampShearStrain005=qhY08.getDampShearStrain005();
                    Double dampShearStrain010=qhY08.getDampShearStrain010();
                    Double dampShearStrain050=qhY08.getDampShearStrain050();
                    Double dampShearStrain100=qhY08.getDampShearStrain100();
                    Double dampShearStrain500=qhY08.getDampShearStrain500();
                    Double dampShearStrain1000=qhY08.getDampShearStrain1000();
                    Double dampShearStrain5000=qhY08.getDampShearStrain5000();
                    Double dampShearStrain10000=qhY08.getDampShearStrain10000();
                    if (StringUtils.isEmpty(specimenCode)&&specimenCode.length()>20){
                        ServerUtil.addErrorMsg(errorMsg, "样品编号", "不多于20个字符");
                    }

                    if (specimenStartDepth!=null&&(specimenStartDepth+"").length()-(specimenStartDepth+"").indexOf(".")-1>2){
                        ServerUtil.addErrorMsg(errorMsg, "取样深度顶点", "小数点后最多两位");
                    }
                    if (specimenEndDepth!=null&&(specimenEndDepth+"").length()-(specimenEndDepth+"").indexOf(".")-1>2){
                        ServerUtil.addErrorMsg(errorMsg, "取样深度底点", "小数点后最多两位");
                    }
                    if (StringUtils.isEmpty(soilName)&&soilName.length()>20){
                        ServerUtil.addErrorMsg(errorMsg, "土层名称", "不多于20个字符");
                    }
                    if (naturalDensity!=null&&(naturalDensity+"").length()-(naturalDensity+"").indexOf(".")-1>2){
                        ServerUtil.addErrorMsg(errorMsg, "密度", "小数点后最多两位");
                    }
                    if (modulusShearStrain005!=null&&(modulusShearStrain005+"").length()-(modulusShearStrain005+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "模量比(G/Gmax)剪应变(γ)（10-4×0.05)", "小数点后最多五位");
                    }
                    if (modulusShearStrain010!=null&&(modulusShearStrain010+"").length()-(modulusShearStrain010+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "模量比(G/Gmax)剪应变(γ)（10-4×0.10)", "小数点后最多五位");
                    }
                    if (modulusShearStrain050!=null&&(modulusShearStrain050+"").length()-(modulusShearStrain050+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "模量比(G/Gmax)剪应变(γ)（10-4×0.50)", "小数点后最多五位");
                    }
                    if (modulusShearStrain100!=null&&(modulusShearStrain100+"").length()-(modulusShearStrain100+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "模量比(G/Gmax)剪应变(γ)（10-4×1.00)", "小数点后最多五位");
                    }
                    if (modulusShearStrain500!=null&&(modulusShearStrain500+"").length()-(modulusShearStrain500+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "模量比(G/Gmax)剪应变(γ)（10-4×5.00)", "小数点后最多五位");
                    }
                    if (modulusShearStrain1000!=null&&(modulusShearStrain1000+"").length()-(modulusShearStrain1000+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "模量比(G/Gmax)剪应变(γ)（10-4×10.00)", "小数点后最多五位");
                    }
                    if (modulusShearStrain5000!=null&&(modulusShearStrain5000+"").length()-(modulusShearStrain5000+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "模量比(G/Gmax)剪应变(γ)（10-4×50.00)", "小数点后最多五位");
                    }
                    if (modulusShearStrain10000!=null&&(modulusShearStrain10000+"").length()-(modulusShearStrain10000+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "模量比(G/Gmax)剪应变(γ)（10-4×100.00)", "小数点后最多五位");
                    }
                    if (dampShearStrain005!=null&&(dampShearStrain005+"").length()-(dampShearStrain005+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "阻尼比(λ)剪应变(γ)（10-4×0.05)", "小数点后最多五位");
                    }
                    if (dampShearStrain010!=null&&(dampShearStrain010+"").length()-(dampShearStrain010+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "阻尼比(λ)剪应变(γ)（10-4×0.10)", "小数点后最多五位");
                    }
                    if (dampShearStrain050!=null&&(dampShearStrain050+"").length()-(dampShearStrain050+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "阻尼比(λ)剪应变(γ)（10-4×0.50)", "小数点后最多五位");
                    }
                    if (dampShearStrain100!=null&&(dampShearStrain100+"").length()-(dampShearStrain100+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "阻尼比(λ)剪应变(γ)（10-4×1.00)", "小数点后最多五位");
                    }
                    if (dampShearStrain500!=null&&(dampShearStrain500+"").length()-(dampShearStrain500+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "阻尼比(λ)剪应变(γ)（10-4×5.00)", "小数点后最多五位");
                    }
                    if (dampShearStrain1000!=null&&(dampShearStrain1000+"").length()-(dampShearStrain1000+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "阻尼比(λ)剪应变(γ)（10-4×10.00)", "小数点后最多五位");
                    }
                    if (dampShearStrain5000!=null&&(dampShearStrain5000+"").length()-(dampShearStrain5000+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "阻尼比(λ)剪应变(γ)（10-4×50.00)", "小数点后最多五位");
                    }
                    if (dampShearStrain10000!=null&&(dampShearStrain10000+"").length()-(dampShearStrain10000+"").indexOf(".")-1>5){
                        ServerUtil.addErrorMsg(errorMsg, "阻尼比(λ)剪应变(γ)（10-4×100.00)", "小数点后最多五位");
                    }

                    String strErrorMsg = errorMsg.toString();
                    if(!strErrorMsg.isEmpty()) {
                        qhY08Vo.setQualityOpinion("Y08的"+strErrorMsg);
                        qhY08Vo.setDataState("1");
                        break;
                    } else {
                        qhY08Vo.setQualityOpinion("系统自动质检通过");
                        qhY08Vo.setDataState("2");
                    }
                    qhY08VoList.add(qhY08Vo);
                }

            }
        } catch (Exception e){
            log.error(e.getMessage());
        }
        return qhY08VoList;
    }
}
